<!DOCTYPE html>
<html lang="en">
<head>
    <th:block th:include="include :: header('角色管理')" />
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px;">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">角色名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">权限字符</label>
                            <div class="layui-input-inline">
                                <input type="text" name="roleSign" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">角色状态</label>
                            <div class="layui-input-inline">
                                <div class="roleSelect" id="roleSelect"></div>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button id="queryArticle" type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>
        <table id="roleTable" lay-filter="roleTableFilter"></table>


    </div>
</div>
</body>
<script type="text/html" id="toolBar">
    {{# if(d.roleSign != 'admin'){ }}
    <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
    {{# } }}
</script>
<script type="text/html" id="currentTableBar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
    </div>
</script>
<th:block th:include="include :: footer" />
<script>

    //多选下拉框
    var demo1 = xmSelect.render({
        el: '#roleSelect',
        radio: true,
        clickClose: true, //点击之后关闭
        theme: {
            color: '#0081ff',
        },
        data: [
            {name: "全部", value: '0',selected:true},
            {name: "停用", value: '-1'},
            {name: "正常", value: '1'}
        ]
    })

    layui.use(['table','form','jquery'],function (){
        var table = layui.table;
        var form = layui.form;

        /**
         * 表格初始化
         */
        table.render({
            elem: "#roleTable",
            url: "/role/admin/tableList",
            toolbar: "#currentTableBar",
            id: "currentRoleTable",
            cols: [
                [
                    {type: 'checkbox'},
                    {field: '', title: '序号',width:60,templet:function (d){
                            return d.LAY_INDEX;
                        }},
                    {field: 'roleName', title: '角色名字',width:180},
                    {field: 'roleSign', title: '角色权限字符', width: 180},
                    {field: 'status', title: '状态', width: 180,templet(d){
                        if (d.status === 1){
                            return '<input type="checkbox" value="'+d.roleId+'" checked="" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">';
                        }else {
                            return '<input type="checkbox" value="'+d.roleId+'" name="open" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">';
                        }}},
                    {field: 'createBy', title: '创建人', width: 180},
                    {field: 'createTime', title: '创建时间', width: 180},
                    {field: 'updateTime', title: '修改时间', width: 180},
                    {field: 'remark', title: '备注', width: 180},
                    {fixed: 'right', title:'操作', toolbar: '#toolBar', width:200}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            parseData: function(res){//将原始数据解析成 table 组件所规定的数据
                console.log(res)
                var result;
                if(this.page.curr){
                    result = res.data.slice(this.limit*(this.page.curr-1),this.limit*this.page.curr);
                }
                else{
                    result=res.data.slice(0,this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        })

        //表格顶部搜索
        form.on('submit(data-search-btn)', function (data) {
            var status = demo1.getValue('valueStr');
            if (status === ''){
                status = 0;
            }
            table.reload('currentRoleTable', {
                url: '/role/admin/queryRoleByCondition'
                , where: {
                    "roleName": data.field.roleName,
                    "status": status,
                    "roleSign": data.field.roleSign
                } //设定异步数据接口的额外参数
                //,height: 300
            });
        })

        /**
         * 顶部操作栏
         */
        table.on('toolbar(roleTableFilter)', function (obj) {
            console.log(obj)
            if (obj.event === 'add') {
                Pt.openModalBox('新增角色','/role/admin/role-add')
            }else if (obj.event === 'delete'){
                var allData = table.checkStatus('currentRoleTable').data;
                if (allData.length > 0){
                    Pt.openConfirmBox("是否删除",['确定','取消'],test,allData);
                }else {
                    Pt.showWarnMsg("请选择要删除的数据")
                }
            }
        })

        function test(aaaa){
            console.log(aaaa);
        }

        /**
         * 监听表格开关状态
         * @param obj
         * @return
         */
        form.on('switch(switchTest)', function(obj){
            const formData = {roleId:Number(this.value), status:this.checked === true ? 1 : -1}
            updateRoleStatus(formData,obj);
        })

        /**
         * 表格右侧功能栏
         */
        table.on('tool(roleTableFilter)',function (obj){
            if (obj.event === 'edit'){
                const url = '/role/admin/role-edit?roleId='+obj.data.roleId;
                Pt.openModalBox('编辑角色',url);
            }else if (obj.event === 'delete'){
                var checkData = table.checkStatus('')
                alert('delete')
            }
        })
    })

    /**
     * 更新角色状态
     * @param data
     * @param obj
     * @return
     */
    function updateRoleStatus(data,obj){
        $.ajax({
            url: "/role/admin/changeStatus",
            type: "post",
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(data),
            success(res){
                if (res.state){
                    Pt.showMsg("操作成功")
                }else {
                    Pt.showErrorMsg(res.msg);
                    //操作失败，还原单选框
                    if (data.status === -1){
                        obj.elem.checked = true;
                    }else {
                        obj.elem.checked = false;
                    }
                    layui.form.render();
                }
            },
            error(res){
                console.log(res)
            }
        })
    }

    function deleteRole(role){
        var roleIds = [];
        for (var i = 0; i < role.length; i++) {
            roleIds.push(role[i].roleId)
        }
        $.ajax({
            url: "/role/admin/deleteRole",
            type: "post",
            dataType: "json",
            //必须加上这段代码
            contentType : "application/json;charsetset=UTF-8",
            data: JSON.stringify(roleIds),
            success(res) {
                if (res.state){
                    layui.table.reload('currentRoleTable');
                    layer.closeAll();
                }
            },
            error(res) {
                console.log(res);
            }
        })
    }
</script>
</html>
